package com.chefeiyue.serverpush;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;

public class Server {

	private static List<SocketIOClient> clients = new ArrayList<SocketIOClient>();

	public static void main(String[] args) throws InterruptedException {
		Configuration config = new Configuration();
		config.setHostname("127.0.0.1");
		config.setPort(9902);

		SocketIOServer server = new SocketIOServer(config);

		server.addConnectListener(new ConnectListener() {

			public void onConnect(SocketIOClient client) {
				System.out.println("Connected: sessionId="
						+ client.getSessionId());
				clients.add(client);
			}
		});

		server.start();

		System.out.println("server started");
		Timer timer = new Timer();

		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				Random random = new Random();
				for (SocketIOClient client : clients) {
					client.sendEvent("pushpoint", new Point(
							random.nextInt(100), random.nextInt(100)));
				}
			}
		}, 1000, 1000);

		Object object = new Object();
		synchronized (object) {
			object.wait();
		}

	}
}
